package com.kai.service.util;

import java.util.Random;

public final class KeyGenerator {

    private static final String CHAR_ARR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new Random();

    /**
     * 生成数字和字母随机字符串
     */
    public static String generateKey(int length) {
        if (length > 0) {
            StringBuilder code = new StringBuilder();
            for (int i = 0; i < length; i++) {
                int temp = RANDOM.nextInt(CHAR_ARR.length());
                code.append(CHAR_ARR.charAt(temp));
            }
            return code.toString();
        }
        return null;
    }

    /**
     * 生成数字随机字符串
     */
    public static String generateNumberKey(int length) {
        if (length > 0) {
            StringBuilder code = new StringBuilder();
            for (int i = 0; i < length; i++) {
                code.append(RANDOM.nextInt(10));
            }
            return code.toString();
        }
        return null;
    }
}
